package ${package.Controller};

import com.baomidou.mybatisplus.core.metadata.IPage;

import ${cfg.packagePath}.common.model.PageInfo;
import ${cfg.packagePath}.common.util.JsonUtil;
import ${cfg.packagePath}.common.model.Rest;
import ${cfg.packagePath}.common.model.PageReq;
import ${package.Entity}.*;
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import java.util.List;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@RestController
<#else>
@Controller
</#if>
<#--@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")-->
@RequestMapping("<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} {</#if>

    @Autowired
<#--    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};-->
    private ${table.serviceName} ${(table.serviceName)?uncap_first};

    @ApiOperation(value = "查询${table.comment!}数据")
    @GetMapping("list")
    public Rest<List${r"<"}${entity}VO>> list(${entity}SearchReq req){
        List<${entity}DTO> list = ${(table.serviceName)?uncap_first}.list(req);
        List<${entity}VO> voList = JsonUtil.listTransform(list, ${entity}VO.class);

        return Rest.success(voList);
    }

    @ApiOperation(value = "查询${table.comment!}分页数据")
    @GetMapping("page")
    public Rest<PageInfo${r"<"}${entity}VO>> page(${entity}SearchReq req, PageReq pageReq){
        IPage<${entity}DTO> iPage = ${(table.serviceName)?uncap_first}.page(req, pageReq);
        PageInfo<${entity}VO> pageInfo = JsonUtil.iPageTransform(iPage, ${entity}VO.class);

        return Rest.success(pageInfo);
    }

    @ApiOperation(value = "新增${table.comment!}")
    @PostMapping("add")
    public Rest<String> add(@RequestBody @Validated ${entity}AddReq req){
        ${(table.serviceName)?uncap_first}.add(req);

        return Rest.success();
    }

    @ApiOperation(value = "删除${table.comment!}")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="${table.comment!}-id",required=true),
    })
    @PostMapping("delete")
    public Rest<String> delete(@RequestParam Long id){
        ${(table.serviceName)?uncap_first}.delete(id);

        return Rest.success();
    }

    @ApiOperation(value = "更新${table.comment!}")
    @PostMapping("update")
    public Rest<String> update(@RequestBody @Validated ${entity}UpdateReq req){
        ${(table.serviceName)?uncap_first}.update(req);

        return Rest.success();
    }

    @ApiOperation(value = "根据id查询${table.comment!}")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="${table.comment!}-id",required=true),
    })
    @GetMapping("get-by-id")
    public Rest<${entity}VO> getById(@RequestParam Long id){
        ${entity}DTO ${(entity)?uncap_first}DTO = ${(table.serviceName)?uncap_first}.getById(id);
        ${entity}VO ${(entity)?uncap_first}VO = new ${entity}VO();
        BeanUtils.copyProperties(${(entity)?uncap_first}DTO, ${(entity)?uncap_first}VO);

        return Rest.success(${(entity)?uncap_first}VO);
    }

}
</#if>